package com.spring.pick2pick;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.pick2pick.common.PageHandler;
import com.spring.pick2pick.common.Util;
import com.spring.pick2pick.dao.CommonDao;
import com.spring.pick2pick.model.AdminItem;
import com.spring.pick2pick.model.CommonItem;

@Controller
public class CommonController {
	
	@Autowired
	CommonDao commonDao;
	
	/** 레이아웃 조회 */
	@RequestMapping(value = "viewLayout", method = RequestMethod.GET)
	public String viewLayout(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		model.addAttribute("list", commonDao.getCommonList());
		return "admin/viewLayout";
	}
	
	/** 레이아웃 수정 */
	@RequestMapping(value = "editCommonOk", method = RequestMethod.POST)
	public String editCommonOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String idCommonStr = request.getParameter("idCommon");
		String commonText = request.getParameter("commonText" + idCommonStr);
		
		if (idCommonStr == null || commonText == null || idCommonStr.equals("") || commonText.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
			return null;
		}
		
		int idCommon = Integer.parseInt(idCommonStr);
		
		CommonItem item = new CommonItem();
		item.setIdCommon(idCommon);
		item.setCommonText(commonText);
		
		int result = commonDao.editCommon(item);
		
		if (result > 0) {
			try {
				String html = "<!doctype html>";
				html += "<head>";
				html += "<meta charset='utf-8'>";
				html += "<script>";
				html += "alert('수정되었습니다.');";
				html += "location.replace('viewLayout');";
				html += "</script>";
				html += "</head>";
				html += "</html>";
				response.setCharacterEncoding("utf-8");
				PrintWriter out = response.getWriter();
				out.print(html);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			Util.getInstance().alertPage("수정에 실패하였습니다.", response);
		}
		
		return null;
	}
	
	/** 기타 게시글관리 리스트 목록 */
	@RequestMapping(value = "listEtc", method = RequestMethod.GET)
	public String listEtc(Locale locale, Model model, HttpServletRequest request) {
		int page = 1;
		int size = 10;
		String pageStr = request.getParameter("page");
		
		if (pageStr != null) {
			page = Integer.parseInt(pageStr);
		}
		
		model.addAttribute("list", commonDao.getEtcList(page, size));
		model.addAttribute("page", PageHandler.getInstance());
		
		return "admin/listEtc";
	}
	
	/** 기타 게시글관리 전시여부 변경 */
	@RequestMapping(value = "displayEtc", method = RequestMethod.GET)
	public String displayEtc(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String pageStr = request.getParameter("page");
		String idStr = request.getParameter("id");
		String isDisplayStr = request.getParameter("isDisplay");
		
		if (pageStr == null || idStr == null || isDisplayStr == null || pageStr.equals("") || idStr.equals("") || isDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int page = Integer.parseInt(pageStr);
			int id = Integer.parseInt(idStr);
			int isDisplay = Integer.parseInt(isDisplayStr);
			
			if (isDisplay == 0) {
				isDisplay = 1;
			} else if (isDisplay == 1) {
				isDisplay = 0;
			}
			
			CommonItem item = new CommonItem();
			item.setIdCommon(id);
			item.setIsDisplay(isDisplay);
			int result = commonDao.editDisplay(item);
			
			if (result > 0) {
				
				String html = "<!doctype html>";
				html += "<head>";
				html += "<meta charset='utf-8'>";
				html += "<script>";
				html += "alert('정상적으로 수정되었습니다.');";
				html += "location.replace('listEtc?page=" + page + "');";
				html += "</script>";
				html += "</head>";
				html += "</html>";
				
				try {
					response.setCharacterEncoding("utf-8");
					PrintWriter out = response.getWriter();
					out.print(html);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("전시여부 수정에 실패하였습니다.", response);
			}
			
		}
		
		return null;
	}
	
	/** 기타 게시글관리 조회 */
	@RequestMapping(value = "viewEtc", method = RequestMethod.GET)
	public String viewEtc(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		String idStr = request.getParameter("id");
		String pageStr = request.getParameter("page");
		
		if (idStr == null || idStr.equals("") || pageStr == null || pageStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int id = Integer.parseInt(idStr);
			int page = Integer.parseInt(pageStr);
			
			CommonItem item = new CommonItem();
			item.setIdCommon(id);
			
			CommonItem result = commonDao.getEtc(item);
			model.addAttribute("item", result);
			model.addAttribute("page", page);
		}
		
		return "admin/viewEtc";
	}
	
	/** 기타 게시글관리 추가 */
	@RequestMapping(value = "addEtc", method = RequestMethod.GET)
	public String addEtc(Locale locale, Model model) {
		return "admin/addEtc";
	}
	
	/** 기타 게시글관리 추가후 리스트 목록 */
	@RequestMapping(value = "addEtcOk", method = RequestMethod.POST)
	public String addEtcOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String commonTitle = request.getParameter("commonTitle");
		String commonText = request.getParameter("commonText");
		
		if (commonTitle == null || commonText == null || commonTitle.equals("") || commonText.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			CommonItem item = new CommonItem();
			item.setCommonTitle(commonTitle);
			item.setCommonText(commonText);
			
			HttpSession session = request.getSession();
			AdminItem loginInfo = (AdminItem) session.getAttribute("ADMIN_INFO");
			item.setIdAdmin(loginInfo.getIdAdmin());
			
			int result = commonDao.addEtc(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listEtc");
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("추가에 실패하였습니다.", response);
			}
		}
		return null;
	}
	
	/** 기타 게시글관리 수정후 리스트 목록 */
	@RequestMapping(value = "editEtcOk", method = RequestMethod.POST)
	public String editEtcOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String commonTitle = request.getParameter("commonTitle");
		String commonText = request.getParameter("commonText");
		String idCommonStr = request.getParameter("idCommon");
		String isDisplayStr = request.getParameter("isDisplay");
		String pageStr = request.getParameter("page");
		
		if (commonTitle == null || commonText == null || idCommonStr == null || pageStr == null || isDisplayStr == null ||
				commonTitle.equals("") || commonText.equals("") || idCommonStr.equals("") || pageStr.equals("") || isDisplayStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int idCommon = Integer.parseInt(idCommonStr);
			int isDisplay = Integer.parseInt(isDisplayStr);
			
			CommonItem item = new CommonItem();
			item.setCommonTitle(commonTitle);
			item.setCommonText(commonText);
			item.setIdCommon(idCommon);
			item.setIsDisplay(isDisplay);
			
			int page = Integer.parseInt(pageStr);
			
			HttpSession session = request.getSession();
			AdminItem loginInfo = (AdminItem) session.getAttribute("ADMIN_INFO");
			item.setIdAdmin(loginInfo.getIdAdmin());
			
			int result = commonDao.editEtc(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listEtc?page=" + page);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("수정에 실패하였습니다.", response);
			}
		}
		return null;
	}
	
	/** 기타 게시글관리 삭제후 리스트 목록 */
	@RequestMapping(value = "deleteEtcOk", method = RequestMethod.POST)
	public String deleteEtcOk(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		String idCommonStr = request.getParameter("idCommon");
		String pageStr = request.getParameter("page");
		
		if (idCommonStr == null || pageStr == null || idCommonStr.equals("") || pageStr.equals("")) {
			Util.getInstance().alertPage("필수입력 항목이 누락되었습니다.", response);
		} else {
			int idCommon = Integer.parseInt(idCommonStr);
			
			CommonItem item = new CommonItem();
			item.setIdCommon(idCommon);
			
			int page = Integer.parseInt(pageStr);
			
			int result = commonDao.deleteEtc(item);
			
			if (result > 0) {
				try {
					response.sendRedirect("listEtc?page=" + page);
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				Util.getInstance().alertPage("삭제에 실패하였습니다.", response);
			}
		}
		return null;
	}
	
}
